const app = getApp();
Page({
  data: {
    showPicker: false,
    old_value: [0,0],
    currentVal: [0,0],
    cate_one: [],
    cate_two: [],
    cate_one_name: '',
    cate_two_name: '',
    categoryid: '',
    categorysubid: '',
  },
  onLoad(opt) {
    this.setData({
      module_id: opt.mid,
    });
    this.loadPosterCate();
  },
  onShow() {
    let resPic = wx.getStorageSync('resPic') && JSON.parse(wx.getStorageSync('resPic'));
    if (resPic) {
    this.setData({ 
        bg: resPic.url,
    });
    }
  },
  onHide() {
    wx.removeStorageSync('resPic');
  },
  onUnload() {
    wx.removeStorageSync('resPic');
  },
  loadPosterCate() {
    let that = this;
    app.util.request({
      url: 'wxapp/poster/moduleCateList',
      success: res => {
        if (res.statusCode === 200) {
          that.setData({
            cate_one: res.data.result,
            cate_two: res.data.result[0].two || [],
            corpid: wx.getStorageSync('qyUserInfo').corpid
          });
        }
      },
    });
  },
  bindChange(e) {
    let cate_one_index = this.data.old_value[0];
    let cate_one = this.data.cate_one;
    if (Number(e.detail.value[0]) !== Number(cate_one_index)) {
      this.setData({
        old_value: e.detail.value,
        cate_two: cate_one[e.detail.value[0]].two,
      });
    }
    this.setData({
      currentVal: e.detail.value,
    });
  },
  disabledClick() {},
  showPickerView() {
    this.setData({
      showPicker: true,
      cate_two: this.data.cate_one[0].two || [],
    });
  },
  hidePicker() {
    this.setData({
      showPicker: false,
    });
  },
  surePicker() {
    let idxs = this.data.currentVal;
    let list1 = this.data.cate_one;
    let list2 = this.data.cate_two;
    this.setData({
      cate_one_name: list1[idxs[0]].title,
      categoryid: list1[idxs[0]].id,
    });
    if (list2.length > 0) {
      this.setData({
        cate_two_name: list2[idxs[1]].title,
        categorysubid: list2[idxs[1]].id,
      });
    } else {
      this.setData({
        cate_two_name: '',
        categorysubid: '',
      });
    }
    this.setData({
      showPicker: false,
    });
  },
  getTitle(e) {
    this.setData({
      title: (e.detail.value).trim(),
    });
  },
  chooseNavigatorUrl(e) {
    if (Number(e.detail.value) === 1) {
      this.setData({
        check_title: '名片',
        wxapp_url: 'common/component/universal-card/card-show',
      });
    } else if (Number(e.detail.value) === 2) {
      this.setData({
        check_title: '首页',
        wxapp_url: '',
      });
    }
  },
  choosePicture() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        let tempFilePaths = res.tempFilePaths[0];
        let sceneName = 'poster';
        let url = 'utility/file/upload';
        if (url.indexOf('http://') === -1 && url.indexOf('https://') === -1) {
          url = app.util.url(url, { type: 'image', thumb: '0' });
        }
        wx.setStorage({
          key: 'uploadUrl',
          data: url,
        });
        wx.navigateTo({
          url: `/common/component/cropper/cropper?sceneName=${sceneName}&src=${tempFilePaths}`,
        });
      },
    });
  },
  clearCash() {
    this.setData({
      bg: '',
    });
    wx.removeStorageSync('resPic');
  },
  addPosterSubmit() {
    let that = this;
    let openid = wx.getStorageSync('userInfo').openid;
    let { module_id, categoryid, categorysubid, title, bg } = that.data;
    if (!bg) {
      wx.showModal({
        title: '提示',
        content: '请上传图片',
        showCancel: false,
      });
      return;
    }
    if (!title) {
      wx.showModal({
        title: '提示',
        content: '海报标题不能为空',
        showCancel: false,
      });
      return;
    } 
    if (!categoryid) {
      wx.showModal({
        title: '提示',
        content: '请选择分类',
        showCancel: false,
      });
      return;
    }
    app.util.request({
      url: 'wxapp/poster/moduleSub',
      data: {
        module_id,
        openid,
        categoryid,
        categorysubid,
        title,
        bg,
        thumb: bg,
        corpid: wx.getStorageSync('qyUserInfo').corpid
      },
      success: res => {
        if (res.statusCode === 200 && res.data.status === 1) {
          wx.showModal({
            title: '提示',
            content: res.data.result.message || '添加成功',
            showCancel: false,
            success: res => {
              if (res.confirm) {
                wx.navigateBack();
              }
            },
          });
        } else {
          wx.showToast({
            title: res.data.result.message || '添加失败',
            icon: 'none',
          });
        }
      },
    });
  },
});
